from BloodSpiderAPI import models

from BloodSpiderModel.DjangoResponseTool import response_dict

from django import forms

from BloodSpiderAPI.apis.platform.utils import utils_model_to_dict



# 平台模型
class PlatformModel(forms.ModelForm):
    class Meta:
        model = models.Platform
        fields = [
            "name",
            "website",
            "logo"
        ]



# 创建平台
def create_platform(request):
    if request.method != "POST":
        return response_dict.response_dict(code=1, message="Method not allowed")
    # 验证请求参数
    form = PlatformModel(request.POST)
    if form.is_valid():
        # 创建平台
        platform = utils_model_to_dict(form.save())
        # 返回平台信息
        return response_dict.response_dict(data=platform, message="平台创建成功")
    else:
        # 返回错误信息
        error = response_dict.get_first_error(form)
        return response_dict.response_dict(code=1, message="Invalid request data", data=error)


# 获取全部平台
def get_all_platforms(request):
    if request.method != "GET":
        return response_dict.response_dict(code=1, message="Method not allowed")
    # 获取全部平台
    platforms = models.Platform.objects.all()
    # 返回平台信息
    return response_dict.response_dict(data=[utils_model_to_dict(platform) for platform in platforms], message="平台查询成功")
